package com.zhuoyue.biz.core.command.cmd.common.faa;

import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.domain.ZigbeeDeviceProperty;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.cmd.common.faa.base.BaseCmd;
import com.zhuoyue.common.constant.ColorfulBulbLampMode;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author wjming
 */
public class ColorfulBulbLamp0Faa0B02 extends BaseCmd {

    /**
     * 区间
     */
    private static final String DURATION = "duration";
    /**
     * 00
     */
    private static final String ZERO_ZERO = "00";

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq, ZigbeeDeviceProperty zigbeeDeviceProperty) {
        String cmd = "";
        String duration;
        if (zigbeeDeviceProperty == null) {
            duration = ZERO_ZERO;
        } else {
            JSONObject property = JSONObject.parseObject(zigbeeDeviceProperty.getProperty());
            if (property.get(DURATION) == null || property.get(DURATION) == "") {
                duration = ZERO_ZERO;
            } else {
                duration = CmdUtils.intToHexTwo((Integer) property.get(DURATION) * 2 + 1);
            }
        }
        String data;
        switch (controlReq.getControlType()) {
            case ControlType.TURN_ON_REQUEST:
                cmd = "0201" +
                        deviceAndGatewayDO.getGatewayMac() +
                        deviceAndGatewayDO.getZigbeeMac() +
                        "0faaff0a1f0000000102000001ff";
                break;
            case ControlType.TURN_OFF_REQUEST:
                cmd = "0201" +
                        deviceAndGatewayDO.getGatewayMac() +
                        deviceAndGatewayDO.getZigbeeMac() +
                        "0faaff06090000000102";
                break;
            case ControlType.SET_BRIGHTNESS_PERCENTAGE_REQUEST:
                if (StringUtils.equals(ZERO_ZERO, duration)) {
                    data = "0FAAFF0A1F0000000102";
                } else {
                    data = "0FAAFF0A1F0000000202";
                }
                cmd = "0201" +
                        deviceAndGatewayDO.getGatewayMac() +
                        deviceAndGatewayDO.getZigbeeMac() +
                        data +
                        "00" + duration +
                        "01" +
                        CmdUtils.intToHexTwo((int) controlReq.getActions().get("brightness"));
                break;
            case ControlType.SET_COLOR_REQUEST:
                if (StringUtils.equals(ZERO_ZERO, duration)) {
                    data = "0faaff0c7f0000000103";
                } else {
                    data = "0faaff0c7f0000000203";
                }
                cmd = "0201" +
                        deviceAndGatewayDO.getGatewayMac() +
                        deviceAndGatewayDO.getZigbeeMac() +
                        data +
                        "00" + duration +
                        "01" +
                        CmdUtils.intToHexTwo((Integer) controlReq.getActions().get("red")) +
                        CmdUtils.intToHexTwo((Integer) controlReq.getActions().get("green")) +
                        CmdUtils.intToHexTwo((Integer) controlReq.getActions().get("blue"));
                break;
            case ControlType.SET_MODE_REQUEST:
                String modeState = controlReq.getActions().get("modeState").toString();
                cmd = "0201"
                        + deviceAndGatewayDO.getGatewayMac() +
                        deviceAndGatewayDO.getZigbeeMac() + "0FAAFF";
                switch (modeState) {
                    case ColorfulBulbLampMode.COLORFUL_GRADIENT:
                        cmd = cmd + "070B000000090101";
                        break;
                    case ColorfulBulbLampMode.COLORFUL_JUMP:
                        cmd = cmd + "090F0000000A01000501";
                        break;
                    case ColorfulBulbLampMode.BREATHING_LAMP:
                        cmd = cmd + "0A7B0000000B0301F00000";
                        break;
                    default:
                        cmd = "";
                        break;
                }
                break;
            default:
                break;
        }

        return cmd;
    }


}
